import Api from "./request.js";
import config from "@r/config";
import { getCurPage, handleParmas } from "./utils.js";

const { monitor } = config;

/** 监测统计
 * @param {object | string} options（非必填）  传入类型为string默认为event_type
 * @param {string} options.event_type（非必填）  事件类型 click | view
 * @param {string} options.page_title(非必填)  页面标题
 *
 * @param {string} event_key（非必填）点击事件名称
 * @param {object} json（非必填）如需keywords或者其他自定义字段可在这里传入。
 */
const dataStatistics = (options, event_key, json = {}) => {
  const user_id = "";
  const unionid = "";
  const openid = "";
  console.error("user_id、unionid、openid需要自己配置");
  const wechat_user_name = monitor.user_name;
  const wechat_appid = monitor.appid;

  /*获取当前路由*/
  const curPage = getCurPage();
  const curParam = curPage.options || curPage.$route.query;
  const _url = curPage.route; //当前页面url

  let _page_title = "";
  try {
    _page_title = curPage.data.pageTitle;
  } catch (e) {}

  // 事件类型
  let event_type = "";
  if (typeof options == "object") {
    event_type = options.event_type || "";
    _page_title = options.page_title || _page_title;
  } else {
    event_type = options;
  }

  const params = {
    type: 3,
    user_id,
    unionid,
    openid,
    page_title: _page_title,
    page_description: undefined,
    page_url: encodeURIComponent(_url),
    page_referer_url: undefined,
    page_param: curParam,
    page_event_key: event_key || undefined, // 当是点击事件的时候，这里需要传点击按钮的文案。
    page_event_type: event_type || "view", // view = 浏览  click = 点击   share = 分享
    wechat_user_name,
    wechat_appid,
    ...json,
  };

  Api.monitor(handleParmas(params)).then(() => {});
};

export { dataStatistics };
